package cn.silver.module.monitor.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.silver.framework.excel.utils.ExcelUtil;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.mybatis.controller.BaseController;
import cn.silver.framework.openapi.model.Result;
import cn.silver.module.monitor.domain.LogOper;
import cn.silver.module.monitor.bean.LogOperBean;
import cn.silver.module.monitor.param.OperParam;
import cn.silver.module.monitor.service.ILogOperService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 操作日志记录
 *
 * @author Silver Zhou
 */
@Validated
@RestController
@RequiredArgsConstructor
@Tag(name = "系统监控-系统操作记录管理")
@RequestMapping("/monitor/operlog")
public class LogOperController extends BaseController<ILogOperService, LogOper, OperParam, LogOperBean> {

    @Override
    @Resource
    public void setBaseService(ILogOperService baseService) {
        this.baseService = baseService;
    }

    /**
     * 导出操作日志记录列表
     */
    @Log(title = "操作日志", operate = OperateEnums.OSS_EXPORT)
    @SaCheckPermission("monitor:operlog:export")
    @PostMapping("/export")
    public void export(OperParam operLog, HttpServletResponse response) {
        List<LogOperBean> list = baseService.selectList(operLog);
        ExcelUtil.exportExcel(list, "操作日志", LogOperBean.class, response);
    }

    /**
     * 清理操作日志记录
     */
    @Log(title = "操作日志", operate = OperateEnums.ALTER_CLEAR)
    @SaCheckPermission("monitor:operlog:remove")
    @DeleteMapping("/clean")
    public Result<Void> clean() {
        baseService.cleanOperLog();
        return Result.ok();
    }
}
